package com.zzx.config;

import com.zzx.interceptor.AuthInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    // 跨域配置（可选）
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE")
                .maxAge(3600);
    }

    // 注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new AuthInterceptor())
                .addPathPatterns("/**")               // 拦截所有API接口
                .excludePathPatterns(                     // 排除路径
                        "/doc.html",                     // Knife4j文档页
                        "/webjars/**",                   // Knife4j静态资源
                        "/v3/api-docs/**",               // OpenAPI描述文件
                        "/swagger-resources/**",         // Swagger资源
                        "/auth/wxLogin",         // 登录接口
                        "/favicon.ico"                   // 图标
                )
                .order(0);  // 执行顺序
    }

    // 静态资源配置（必须添加Knife4j资源映射）
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}
